package org.goj.user.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.goj.common.entity.user.Role;
import org.goj.common.entity.user.RoleResource;
import org.goj.common.entity.user.UserRole;
import org.goj.user.mapper.RoleMapper;
import org.goj.user.mapper.UserRoleMapper;
import org.goj.user.service.RoleResourceService;
import org.goj.user.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限授予 服务实现类
 * </p>
 *
 * @author CK
 * @since 2020-12-16
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleResourceService roleResourceService;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertRole(Integer roleId, String roleName, String defunct, List<Integer> resourceIds) {
        Role role = new Role();
        if (roleId != -1) {
            role.setRoleId(roleId);
        }
        role.setRoleName(roleName);
        role.setDefunct(defunct);
        if (roleId == -1) {
            boolean b = roleMapper.insertRole(role);
            Assert.isTrue(b, "添加角色失败");
        } else {
            boolean b = super.updateById(role);
            Assert.isTrue(b, "更新角色失败");
        }
        if (roleId != -1) {
            QueryWrapper<RoleResource> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", roleId);
            roleResourceService.remove(queryWrapper);
        }
        boolean b = roleResourceService.saveBatch(resourceIds.stream().map(t -> {
            RoleResource resource = new RoleResource();
            resource.setRoleId(role.getRoleId()).setResourceId(t);
            return resource;
        }).filter(t -> t.getResourceId() < 1000).collect(Collectors.toList()));
        Assert.isTrue(b, "添加角色失败，请分配角色权限");
        return true;
    }

    @Override
    public Role roleById(Integer roleId) {
        Role role = super.getById(roleId);
        QueryWrapper<RoleResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("resource_id").eq("role_id", roleId);
        role.setResourceIds(roleResourceService.list(
                queryWrapper).stream().map(RoleResource::getResourceId).collect(Collectors.toList()));
        return role;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRole(Integer roleId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        Assert.isTrue(userRoleMapper.selectList(queryWrapper).size() < 1, "该角色存在用户使用，禁止删除");
        int i = roleMapper.deleteById(roleId);
        Assert.isTrue(i == 1, "删除角色失败");
        QueryWrapper<RoleResource> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_id", roleId);
        boolean remove = roleResourceService.remove(queryWrapper1);
        Assert.isTrue(remove, "删除角色失败");
        return true;
    }
}
